package com.tanhua.sso.controller;

import com.tanhua.common.pojo.User;
import com.tanhua.sso.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @ClassName: UserController
 * @Author:王晨晗
 * @Description: 用户模块
 * @Date: 2021/12/25/18:56
 */
@RestController
@RequestMapping("user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    //手机号登录
    @PostMapping("login")
    public ResponseEntity login(@RequestBody Map<String , String> map){
        //获取前端传递的手机号
        String phone = map.get("phone");
        //调用userservice的发送验证码功能
        userService.sendCode(phone);
        //判断返回结果是否为空 , 空则表示发送成功
        return ResponseEntity.ok(null);
    }

    //验证码校验
    @PostMapping("loginVerification")
    public ResponseEntity checkCode(@RequestBody Map<String , String> map){
        //获取手机号和验证码
        String phone = map.get("phone");
        String code = map.get("verificationCode");
        //调用service校验验证码
        Map<String , Object> result = userService.checkCode(phone , code);
        //返回校验结果
        return ResponseEntity.ok(result);
    }

    //解析token的接口
    @GetMapping("{token}")
    public User checkCode(@PathVariable("token") String token){
        //调用service方法解析token
        User user = userService.queryToken(token);
        return user;
    }

}
